package com.it.nongchanping.service.impl;

import com.it.nongchanping.entity.Announcement;
import com.it.nongchanping.mapper.AnnouncementMapper;
import com.it.nongchanping.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 公告服务实现类
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {
    
    @Autowired
    private AnnouncementMapper announcementMapper;
    
    @Override
    public List<Announcement> getAllAnnouncements() {
        return announcementMapper.findAll();
    }
    
    @Override
    public Announcement getAnnouncementById(Long id) {
        return announcementMapper.findById(id);
    }
    
    @Override
    public List<Announcement> getPublishedAnnouncements() {
        return announcementMapper.findByStatus("发布");
    }
    
    @Override
    public List<Announcement> getLatestAnnouncements(int limit) {
        return announcementMapper.findLatest(limit);
    }
    
    @Override
    public boolean saveAnnouncement(Announcement announcement) {
        announcement.setCreateTime(LocalDateTime.now());
        announcement.setUpdateTime(LocalDateTime.now());
        return announcementMapper.insert(announcement) > 0;
    }
    
    @Override
    public boolean updateAnnouncement(Announcement announcement) {
        announcement.setUpdateTime(LocalDateTime.now());
        return announcementMapper.update(announcement) > 0;
    }
    
    @Override
    public boolean deleteAnnouncement(Long id) {
        return announcementMapper.deleteById(id) > 0;
    }

    @Override
    public long countAllAnnouncements() {
        return announcementMapper.countAll();
    }
} 